<?
include_once('tpb-db.php');
include_once('tpb-email.php');
include_once('tpb-util.php');
include_once('tpb-daylife-auth.php');

$base_instances = '/var/www/endeepen/instances';

$action = (isset($_REQUEST['action']))? $_REQUEST['action'] : NULL;
$email_address = (isset($_REQUEST['email_address']))? $_REQUEST['email_address'] : NULL;
$ticket = (isset($_REQUEST['ticket']))? $_REQUEST['ticket'] : NULL;
$webuser_id = (isset($_REQUEST['webuser_id']))? $_REQUEST['webuser_id'] : NULL;
$return_url = (isset($_REQUEST['return_url']))? $_REQUEST['return_url'] : 'http://www.daylife.com';
$subdomain = (isset($_REQUEST['subdomain']))? $_REQUEST['subdomain'] : NULL;
$cname = (isset($_REQUEST['cname']))? $_REQUEST['cname'] : NULL;
$accesskey=$sharesecret=$source_filter_id='';

#if ($action === NULL) 
#	header( 'Location: ' . $return_url ) ;


if ($action === 'invite' && $email_address != NULL) {
	#echo 'I AM INSIDE invite';
	$ticket = getNewTicket($email_address);
	insertNewInvite($email_address, $ticket);
	sendInviteEmail($email_address, $ticket);
}
else if ($action === 'updateInvite' && 
			$ticket != NULL && 
			($webuser_id !=NULL || $email_address != NULL)) 
{
	#echo 'I AM INSIDE updateinvite';
	
	updateInviteByTicket($ticket, 
						$email_address, 
						microtime(),
						$webuser_id, 
						$subdomain,
						$cname); 
	
} 
else if ($action === 'login' && $email_address != NULL) 
{
	$password = (isset($_REQUEST['password']))? $_REQUEST['password'] : '';
	
	#print 'I AM INSIDE LOGIN' . $email_address . $password;
		
	$result = daylifeAuth($email_address, base64_encode((string)md5($password, true)));

	if ($result && $result['code'] === '1') {
		if ($ticket) {
			#update the ticket by ticket ID
			echo 'I found a ticket';
			$ticket = $ticket;
			$webuser_id = $result['payload']['webuser_id'];
            $accesskey = $result['payload']['accesskey'];
            $sharedsecret = $result['payload']['sharedsecret'];
			
			$result = updateInviteByTicket($ticket, 
								$email_address, 
								microtime(),
								$webuser_id, 
								$subdomain,
								$cname);
            #$return_url = "http://$subdomain.endeepen.com/setup?accesskey=$accesskey&sharedsecret=$sharedsecret";
            $return_url = "http://$subdomain.endeepen.com/topic1/Barack+Obama?adminmode=1";

            copyInstance($subdomain);

			if (!$result) 
				$return_url = 'http://labs.daylife.com';
		}	
		#verify the cname and go to setup	
	}
	else if ($result && $result['code'] === '-1') {
        $return_url = $_SERVER['HTTP_REFERER'] . "&error=-1";
		#exit(0);
	}
	else if ($result && $result['code'] === '-2') {
        $return_url = $_SERVER['HTTP_REFERER'] . "&error=-1";
		#exit(0);
	}
}

function copyInstance($subdomain) {
    global $base_instances;
    $orig = $base_instances . "/base";
    $dest = $base_instances . "/$subdomain";
    if (is_dir($dest))
        return 2; //already exists
    try {
        full_copy($orig, $dest);
    }
    catch(Exception $e){
    }
    

}

function full_copy( $source, $target )
    {
        if ( is_dir( $source ) )
        {
            @mkdir( $target );
           
            $d = dir( $source );
           
            while ( FALSE !== ( $entry = $d->read() ) )
            {
                if ( $entry == '.' || $entry == '..' )
                {
                    continue;
                }
               
                $Entry = $source . '/' . $entry;           
                if ( is_dir( $Entry ) )
                {
                    full_copy( $Entry, $target . '/' . $entry );
                    continue;
                }
                copy( $Entry, $target . '/' . $entry );
            }
           
            $d->close();
        }else
        {
            copy( $source, $target );
        }
    }

#echo 'I AM INSIDE nowhere';
//add all if-else(s) here for actions'
header( 'Location: ' . $return_url ) ;

?>
